package com.wc.codeforces.思维.Turtle_Fingers_Count_the_Values_of_k;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/6/25 16:18
 * @description https://codeforces.com/problemset/problem/1933/C
 */
public class Main {
    /**
     * 思路： 2^20 > 1e6
     * 2 < a, b < 100
     * 记得开long
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int INF = 1 << 20;
    static long a, b, l;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            solve();
        }
        out.flush();
    }

    static void solve() {
        a = sc.nextInt();
        b = sc.nextInt();
        l = sc.nextInt();
        Set<Long> set = new HashSet<>();
        // a ^ i
        for (long A = 1; A <= l; A *= a) {
            // b ^ j
            for (long B = 1; B <= l; B *= b) {
                long k = l / (A * B);
                if (k * A * B == l) set.add(k);
            }
        }
        out.println(set.size());
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
